﻿/*
    Copyright (C) 2008, wj2929, wj2929@gmail.com

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    *KEEP THIS HEADER INFORMATION*
*/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using mjRegister.Entity;
using System.IO;

using System.Xml;
using System.Xml.Linq;

namespace mjRegister.BAL
{
    //本地账户分组
    public class AccountGroup
    {
        public static string GetSystemGroupName(ActiveStatus activeStatus)
        {
            string systemGroupName = string.Empty;
            switch (activeStatus)
            {
                case ActiveStatus.NoVerifyActive:
                    systemGroupName = SystemGroup.未验证组.ToString();
                    break;
                case ActiveStatus.NoActive:
                    systemGroupName = SystemGroup.未激活或账户错误组.ToString();
                    break;
                case ActiveStatus.AlreadyActive:
                    systemGroupName = SystemGroup.已验证组.ToString();
                    break;
            }
            return systemGroupName;
        }

        public static void Add(AccountGroupInfo accountGroupInfo)
        {
            XDocument xmlAccountGroup = XDocument.Load(GetAccountGroupXmlFile());

            XElement xeAccountGroups = xmlAccountGroup.Element("AccountGroups");

            xeAccountGroups.Add(new XElement("AccountGroup",
                new XElement("Id", accountGroupInfo.Id.ToString()),
                new XElement("Name", accountGroupInfo.Name)
                )
            );

            lock (xmlAccountGroup)
            {
                xmlAccountGroup.Save(GetAccountGroupXmlFile());
            }
        }

        public static AccountGroupInfo GetAccountGroupInfoById(Guid Id)
        {
            AccountGroupInfo accountGroupInfo = new AccountGroupInfo();

            XDocument xmlAccountGroup = XDocument.Load(GetAccountGroupXmlFile());
            var xeAccountGroups = xmlAccountGroup.Root.Elements("AccountGroup").Where(g => g.Element("Id").Value == Id.ToString());
            foreach (XElement xeAccountGroup in xeAccountGroups)
            {
                accountGroupInfo = new AccountGroupInfo
                {
                    Id = new Guid(xeAccountGroup.Element("Id").Value),
                    Name = xeAccountGroup.Element("Name").Value
                };
                break;
            }

            return accountGroupInfo;
        }


        public static IList<AccountGroupInfo> GetAccountGroupInfos()
        {
            IList<AccountGroupInfo> accountGroupInfos = new List<AccountGroupInfo>();

            XDocument xmlAccountGroup = XDocument.Load(GetAccountGroupXmlFile());
            var xeAccountGroups = xmlAccountGroup.Root.Elements("AccountGroup");
            foreach (XElement xeAccountGroup in xeAccountGroups)
            {
                accountGroupInfos.Add(new AccountGroupInfo
                {
                    Id =  new Guid(xeAccountGroup.Element("Id").Value),
                    Name = xeAccountGroup.Element("Name").Value
                });
            }
            return accountGroupInfos;
        }

        public static bool Edit(AccountGroupInfo accountGroupInfo)
        {
            XDocument xmlAccountGroup = XDocument.Load(GetAccountGroupXmlFile());

            var xeAccountGroups = xmlAccountGroup.Root.Elements("AccountGroup").Where(g => g.Element("Id").Value == accountGroupInfo.Id.ToString());

            if (xeAccountGroups.Count() > 0)
            {
                XElement xeAccountGroup = xeAccountGroups.ToList()[0];
                xeAccountGroup.Element("Name").Value = accountGroupInfo.Name;
                lock (xmlAccountGroup)
                {
                    xmlAccountGroup.Save(GetAccountGroupXmlFile());
                }
                return true;
            }
            return false;
        }

        public static void Delete(AccountGroupInfo accountGroupInfo)
        {
            XDocument xmlAccountGroup = XDocument.Load(GetAccountGroupXmlFile());

            var xeAccountGroups = xmlAccountGroup.Root.Elements("AccountGroup").Where(g => g.Element("Id").Value == accountGroupInfo.Id.ToString());

            if (xeAccountGroups.Count() > 0)
            {
                XElement xeAccountGroup = xeAccountGroups.ToList()[0];
                Account.DeleteByAccountGroup(new AccountGroupInfo
                {
                    Id = new Guid(xeAccountGroup.Element("Id").Value),
                    Name = xeAccountGroup.Element("Name").Value
                });
                xeAccountGroup.Remove();
                lock (xmlAccountGroup)
                {
                    xmlAccountGroup.Save(GetAccountGroupXmlFile());
                }
            }
        }

        public static void CreateAccountGroupXmlFile()
        {
            XElement xmlAccounts = new XElement("AccountGroups");
            using (XmlTextWriter writer = new XmlTextWriter(GetAccountGroupXmlFile(), Encoding.UTF8))
                xmlAccounts.WriteTo(writer);
        }

        public static bool ExistAccountGroup(AccountGroupInfo accountGroupInfo)
        {
            XDocument xmlAccountGroup = XDocument.Load(GetAccountGroupXmlFile());

            var d = xmlAccountGroup.Root.Elements("AccountGroup").Where(g => g.Element("Name").Value == accountGroupInfo.Name);

            return d.Count() > 0;
        }

        static string GetAccountGroupXmlFile()
        {
            return Path.Combine(Common.GetConfigPath(), "accountgroup.xml");
        }

        public static bool CheckAccountGroupXmlFileExist()
        {
            return File.Exists(GetAccountGroupXmlFile());
        }


    }
}
